<?php

namespace App\Admin\Controllers;

use App\Models\VeidooGrades;
use App\Models\Veidoo;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;


class VeidooGradesController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('指标体系')
            ->description('维度等级')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('指标体系')
            ->description('维度等级信息')
            ->body($this->detail($id));

    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
        ->header('指标体系')
        ->description('编辑维度等级')
        ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('指标体系')
            ->description('新建维度等级')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new VeidooGrades);

        $grid->id('Id')->sortable();
        $grid->column('veidoo.name','所属维度');
        $grid->max('得分最大值');
        $grid->min('得分最小值');
        $grid->grade('等级');

        $grid->paginate(10);
//        $grid->disableFilter();
        $grid->actions(function ($actions) {
            $actions->disableView();
        });

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $veioos = Veidoo::get();
            $veioo_arr = [];

            foreach ($veioos as $veioo) {
                $veioo_arr[$veioo->id] = $veioo->name;
            }
            $filter->equal('veidoo_id', '所属维度')->select($veioo_arr);

        });
        $grid->actions(function ($actions) {
//            $actions->disableDelete();
//            $actions->disableEdit();
            $actions->disableView();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(VeidooGrades::findOrFail($id));


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new VeidooGrades());

        $veioos = Veidoo::get();
        $veioo_arr = [];

        foreach ($veioos as $veioo) {
            $veioo_arr[$veioo->id] = $veioo->name;
        }

        $form->select('veidoo_id', trans('维度'))->options($veioo_arr)->setWidth(3, 2);

        $form->number('max','分数最大值');
        $form->number('min','分数最小值');
        $form->select('grade','等级')->options(['A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D'])->setWidth(2, 2);

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        return $form;
    }
}
